package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.web.http.HttpContentType;
import com.ts.api.http.org.entity.apply.OrgApplyLongYanQiNuoXiAn;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMaskConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskApply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMaskDataExchange;

import java.util.ArrayList;

/**
 * 安心分期-龙岩市企诺API全量15 西安
 *
 * @author 李源聪
 */
@ApiPlan(productId = "358", type = ApiPlanConvertType.ORG_PHONE_MASK)
public class ApiOrgPhoneMaskLongYanQiNuoXiAn implements ApiOrgPhoneMaskConvertHandler {
    private static final String KEY = "19D76A5FABE21C32";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) throws Exception {
        apiDataExchangeParameter.setHttpContentType(HttpContentType.GET);
        apiDataExchangeParameter.setExtendUrl("?octet_mobile=" + apiDataExchangeParameter.getPhoneMask());
        return MapUtils.newHashMap(newMap -> newMap.put("data", apiDataExchangeParameter.getPhoneMask()));
    }

    @Override
    public RespOrgPhoneMaskDataExchange respDataExchange(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMaskDataExchange.ofFail();
        }
        return RespOrgPhoneMaskDataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) throws Exception {
        OrgApplyLongYanQiNuoXiAn orgApplyLongYanQiNuoXiAn = new OrgApplyLongYanQiNuoXiAn();
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        orgApplyLongYanQiNuoXiAn.setUserName(apiApplyParameter.getUserName());
        orgApplyLongYanQiNuoXiAn.setPhone(apiApplyParameter.getPhone());
        orgApplyLongYanQiNuoXiAn.setSex(apiFieldConverter.getSexText());
        orgApplyLongYanQiNuoXiAn.setAge(apiFieldConverter.getAge());
        orgApplyLongYanQiNuoXiAn.setLoanAmount(apiFieldConverter.getLoanAmountInt());
        orgApplyLongYanQiNuoXiAn.setCity(apiApplyParameter.getCity());
        orgApplyLongYanQiNuoXiAn.setOverdue(apiFieldConverter.hasOverdueToStr());
        orgApplyLongYanQiNuoXiAn.setProvidentFund(apiFieldConverter.hasProvidentFundToStr());
        orgApplyLongYanQiNuoXiAn.setSocialSecurity(apiFieldConverter.hasSocialSecurityToStr());
        orgApplyLongYanQiNuoXiAn.setSesameCredit(apiFieldConverter.getSesameCreditNumber());
        orgApplyLongYanQiNuoXiAn.setRealEstateInfo(apiFieldConverter.hasRealEstateInfoToStr());
        orgApplyLongYanQiNuoXiAn.setCarInfo(apiFieldConverter.hasCarInfoToStr());
        orgApplyLongYanQiNuoXiAn.setInsurancePolicy(apiFieldConverter.hasInsurancePolicyToStr());
        orgApplyLongYanQiNuoXiAn.setCareer(apiFieldConverter.hasCareerToStr());
        orgApplyLongYanQiNuoXiAn.setRemarks(apiFieldConverter.getExtendInfo());
        ArrayList<OrgApplyLongYanQiNuoXiAn> orgApplyLongYanQiNuoXiAns = new ArrayList<>();
        orgApplyLongYanQiNuoXiAns.add(orgApplyLongYanQiNuoXiAn);
        return JsonUtils.toJson(MapUtils.newHashMap(newMap -> {
            newMap.put("key", KEY);
            newMap.put("Customermodels", orgApplyLongYanQiNuoXiAns);
        }));
    }

    @Override
    public RespOrgPhoneMaskApply respApply(String respJson) throws Exception {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 200)) {
            return RespOrgPhoneMaskApply.ofFail();
        }
        return RespOrgPhoneMaskApply.ofSuc();
    }
}
